home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / misc / math / fpxobj.cpt / FP XObject / FP_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-04  |  11.2 KB  |  255 lines

  1. /*====================================================================*/
  2. /*                                                                    */
  3. /*    FP_main.c:  XObject interface to our floating-point package.    */
  4. /*                Hopefully we can put all of the XObject-ish stuff   */
  5. /*                in this file and keep everything else generic.      */
  6. /*                                                                    */
  7. /*    Copyright ⌐ 1992 The Regents of the University of California.   */
  8. /*                                                                    */
  9. /*    3/21/92    SK    Just getting started...                        */
  10. /*    3/31/92    SK    First functional version (sine function)       */
  11. /*    4/4/92     SK    Adding other functions...                      */
  12. /*                                                                    */
  13. /*====================================================================*/
  14.  
  15. #include <sane.h>
  16.  
  17. /*-----------------------------------------------*/
  18. /* Stuff currently missing from the SANE header: */
  19. /*-----------------------------------------------*/
  20. #define exp21(x)   _elems1(FEXP21X,x)
  21. #define log21(x)   _elems1(FLOG21X,x)
  22. #define ln1(x)     _elems1(FLN1X,x)
  23. #define ln(x)      _elems1(FLNX,x)
  24.  
  25.  
  26. /*-------------------------------------------------------------------*/
  27. /*  The XObject include file contains macros that make it easier to  */
  28. /*  declare the structures necessary to build an XObject.            */
  29. /*  This header is supplied by MacroMind/Paracomp in the XObject     */
  30. /*  developer's toolkit.                                             */
  31. /*-------------------------------------------------------------------*/
  32. #include <XObject.h>
  33. #define TY_EXTENDED 9    /* 80-bit extended float passed as variable arg */
  34.  
  35. enum { FN_Log2, FN_Ln, FN_Ln1, FN_Exp2, FN_Exp, FN_Exp1, FN_Cos,
  36.        FN_Sin, FN_Tan, FN_Atan, FN_RandomX, FN_Log21, FN_Exp21 };
  37.        
  38. /*----------------------------------------------------------------------*/
  39. /*  This declaration defines the structure of the data in the XObject,  */
  40. /*  that is, each instance of the XObject will have these fields for    */
  41. /*  data storage. Note that XClassBegin and XClassEnd are macros        */
  42. /*  defined in XObject.h                                                */
  43. /*----------------------------------------------------------------------*/
  44. XClassBegin {
  45.    long temp;   /* I don't know what data I need yet, so this is a placeholder! */
  46.    } XClassEnd FPObj, FPObjRec;
  47.    
  48.    
  49. /*----------------------------------------------------------------------*/
  50. /*  Function prototypes                                                 */
  51. /*----------------------------------------------------------------------*/
  52. void main(void);
  53. pascal long MethodNew(MsgId message, FPObj me);
  54. pascal long MethodLog2(long argc, TyValuePtr argv, MsgId message, FPObj me);
  55. pascal long MethodLn(long argc, TyValuePtr argv, MsgId message, FPObj me);
  56. pascal long MethodLn1(long argc, TyValuePtr argv, MsgId message, FPObj me);
  57. pascal long MethodExp2(long argc, TyValuePtr argv, MsgId message, FPObj me);
  58. pascal long MethodExp(long argc, TyValuePtr argv, MsgId message, FPObj me);
  59. pascal long MethodExp1(long argc, TyValuePtr argv, MsgId message, FPObj me);
  60. pascal long MethodCos(long argc, TyValuePtr argv, MsgId message, FPObj me);
  61. pascal long MethodSin(long argc, TyValuePtr argv, MsgId message, FPObj me);
  62. pascal long MethodTan(long argc, TyValuePtr argv, MsgId message, FPObj me);
  63. pascal long MethodAtan(long argc, TyValuePtr argv, MsgId message, FPObj me);
  64. pascal long MethodRandomX(long argc, TyValuePtr argv, MsgId message, FPObj me);
  65. pascal long MethodLog21(long argc, TyValuePtr argv, MsgId message, FPObj me);
  66. pascal long MethodExp21(long argc, TyValuePtr argv, MsgId message, FPObj me);
  67.  
  68.  
  69. /*----------------------------------------------------------------------*/
  70. /*  main() provides the entry point for our code resource. It contains  */
  71. /*  the message table, which describes the methods and arguments that   */
  72. /*  callers can use, and provides a jump table for the C code to jump   */
  73. /*  to those messages. Note that XMessagesBegin, XMessage,              */
  74. /*  XMessageEnd, XMethodsBegin, XMethod, and XMethodsEnd are all        */
  75. /*  macros that provide the assembly code to correctly build the entry  */
  76. /*  point for the code resource. See XObject.h for more info.           */
  77. /*  WARNING: MAIN MUST BE THE FIRST FUNCTION IN THIS FILE. OTHERWISE    */
  78. /*  THE ENTRY CODE FOR THE CODE RESOURCE WON'T BE SET UP RIGHT!!!       */
  79. /*----------------------------------------------------------------------*/
  80. void main(void)
  81. {
  82.    /*--- message name table ---*/
  83.    XMessagesBegin
  84.       XMessage "\p-- FP, an XObject to provide advanced floating point functions.";
  85.       XMessage "\p-- by Scott Kelley 4/4/92";
  86.       XMessage "\p-- Copyright ⌐ 1992 The Regents of the University of California"
  87.       XMessage "\pI mNew     -- create a new instance";
  88.       XMessage "\pV mLog2    -- computes the base-2 logarithm of x";
  89.       XMessage "\pV mLn      -- computes the natural logarithm of x";
  90.       XMessage "\pV mLn1     -- computes the natural logarithm of (1+x)";
  91.       XMessage "\pV mExp2    -- computes 2^x";
  92.       XMessage "\pV mExp     -- computes e^x";
  93.       XMessage "\pV mExp1    -- computes (e^x)-1";
  94.       XMessage "\pV mCos     -- computes the cosine of x";
  95.       XMessage "\pV mSin     -- computes the sine of x";
  96.       XMessage "\pV mTan     -- computes the tangent of x";
  97.       XMessage "\pV mAtan    -- computes the arctangent of x";
  98.       XMessage "\pV mRandomX -- computes a pseudorandom value with x as seed";
  99.       XMessage "\pV mLog21   -- computes log base 2 of (1+x)";
  100.       XMessage "\pV mExp21   -- computes (2^x)-1";
  101.    XMessagesEnd
  102.    
  103.    /*--- method dispatch table ---*/
  104.    XMethodsBegin
  105.       XMethod MethodNew;
  106.       XMethod MethodLog2;
  107.       XMethod MethodLn;
  108.       XMethod MethodLn1;
  109.       XMethod MethodExp2;
  110.       XMethod MethodExp;
  111.       XMethod MethodExp1;
  112.       XMethod MethodCos;
  113.       XMethod MethodSin;
  114.       XMethod MethodTan;
  115.       XMethod MethodAtan;
  116.       XMethod MethodRandomX;
  117.       XMethod MethodLog21;
  118.       XMethod MethodExp21;
  119.    XMethodsEnd
  120. }
  121.  
  122.  
  123.  
  124. /*-----------------------------------------------------------------------*/
  125. /*  mNew is called AFTER the generic instance allocation, so we already  */
  126. /*  have an instance, but we have to resize it according to what we      */
  127. /*  need in terms of storage. We also do any other object initialization */
  128. /*  at this point. Returns zero for successful initialization, non-zero  */
  129. /*  for any errors.                                                      */
  130. /*-----------------------------------------------------------------------*/
  131. pascal long MethodNew(MsgId message, FPObj me)
  132. {    short er;  /* returns possible error from memory manager */
  133.  
  134.      XMethodOpen(me);
  135.      SetHandleSize((Handle)me, sizeof(**me));
  136.      if (!(er=MemError())) {
  137.        /*--- memory alloc ok, now do other init ---*/
  138.        }
  139.      XMethodClose(me);
  140.      return (er);
  141. }
  142.  
  143.  
  144. /*--------------------------------------------------------------------*/
  145. /* Allocate a handle to an extended floating point number, and return */
  146. /* that handle. Returns 0 if the handle can't be allocated.           */
  147. /*--------------------------------------------------------------------*/
  148. Handle extended_to_handle(extended num)
  149. { extended *p;
  150.   Handle h=NewHandle(sizeof(num));
  151.   if (h==NULL) return h;
  152.   p=(extended *)*h;
  153.   *p=num;
  154.   return h;
  155. }
  156.  
  157.  
  158. /*---------------------------------------------------------------------------*/
  159. /*  Trig function attempt: the sine function. Passing floating point args    */
  160. /*  as variable parameters...                                                */
  161. /*---------------------------------------------------------------------------*/
  162. pascal long float_methods(int fn, long argc, TyValuePtr argv, MsgId message, FPObj me)
  163. { extended x;
  164.   extended *pargument,*presult;
  165.   Handle h;
  166.   TyValue rv;
  167.   XMethodOpen(me);
  168.   /*--- make sure we have just one argument ---*/
  169.   if (argc!=1) {
  170.     rv.tvType=4;
  171.     rv.tvValue=333333;
  172.     goto EXIT;
  173.     }
  174.     
  175.   /*--- switch based on argument type ---*/
  176.   switch(argv[1].tvType) {
  177.     case TY_EXTENDED:
  178.       pargument=*((extended **)argv[1].tvValue);
  179.       x=*pargument;
  180.       rv.tvType=TY_EXTENDED;
  181.       switch(fn) {
  182.         case FN_Log2:    rv.tvValue=(long)extended_to_handle(log2(x)); break;
  183.         case FN_Ln:      rv.tvValue=(long)extended_to_handle(ln(x)); break;
  184.         case FN_Ln1:     rv.tvValue=(long)extended_to_handle(ln1(x)); break;
  185.         case FN_Exp2:    rv.tvValue=(long)extended_to_handle(exp2(x)); break;
  186.         case FN_Exp:     rv.tvValue=(long)extended_to_handle(exp(x)); break;
  187.         case FN_Exp1:    rv.tvValue=(long)extended_to_handle(exp1(x)); break;
  188.         case FN_Cos:     rv.tvValue=(long)extended_to_handle(cos(x)); break;
  189.         case FN_Sin:     rv.tvValue=(long)extended_to_handle(sin(x)); break;
  190.         case FN_Tan:     rv.tvValue=(long)extended_to_handle(tan(x)); break;
  191.         case FN_Atan:    rv.tvValue=(long)extended_to_handle(atan(x)); break;
  192.         case FN_RandomX: rv.tvValue=(long)extended_to_handle(randomx(&x)); break;
  193.         case FN_Log21:   rv.tvValue=(long)extended_to_handle(log21(x)); break;
  194.         case FN_Exp21:   rv.tvValue=(long)extended_to_handle(exp21(x)); break;
  195.         }
  196.       break;
  197.     default:
  198.       rv.tvType=4;
  199.       rv.tvValue=444444;
  200.     } /* switch */
  201.  
  202.   EXIT:  
  203.   /*--- set return values. Filter for bad handles ---*/
  204.   if ((rv.tvType==TY_EXTENDED)&&(rv.tvValue==(long)NULL)) {
  205.     rv.tvType=TY_LONGINT;  rv.tvValue=999999;
  206.     }
  207.   argv[0].tvType=rv.tvType;
  208.   argv[0].tvValue=rv.tvValue;
  209.   XMethodClose(me);
  210.   /*--- return value is ignored ---*/
  211.   return 0;
  212. }
  213.  
  214.  
  215. pascal long MethodLog2(long argc, TyValuePtr argv, MsgId message, FPObj me)
  216. { float_methods(FN_Log2,argc,argv,message,me); }
  217.  
  218. pascal long MethodLn(long argc, TyValuePtr argv, MsgId message, FPObj me)
  219. { float_methods(FN_Ln,argc,argv,message,me); }
  220.  
  221. pascal long MethodLn1(long argc, TyValuePtr argv, MsgId message, FPObj me)
  222. { float_methods(FN_Ln1,argc,argv,message,me); }
  223.  
  224. pascal long MethodExp2(long argc, TyValuePtr argv, MsgId message, FPObj me)
  225. { float_methods(FN_Exp2,argc,argv,message,me); }
  226.  
  227. pascal long MethodExp(long argc, TyValuePtr argv, MsgId message, FPObj me)
  228. { float_methods(FN_Exp,argc,argv,message,me); }
  229.  
  230. pascal long MethodExp1(long argc, TyValuePtr argv, MsgId message, FPObj me)
  231. { float_methods(FN_Exp1,argc,argv,message,me); }
  232.  
  233. pascal long MethodCos(long argc, TyValuePtr argv, MsgId message, FPObj me)
  234. { float_methods(FN_Cos,argc,argv,message,me); }
  235.  
  236. pascal long MethodSin(long argc, TyValuePtr argv, MsgId message, FPObj me)
  237. { float_methods(FN_Sin,argc,argv,message,me); }
  238.  
  239. pascal long MethodTan(long argc, TyValuePtr argv, MsgId message, FPObj me)
  240. { float_methods(FN_Tan,argc,argv,message,me); }
  241.  
  242. pascal long MethodAtan(long argc, TyValuePtr argv, MsgId message, FPObj me)
  243. { float_methods(FN_Atan,argc,argv,message,me); }
  244.  
  245. pascal long MethodRandomX(long argc, TyValuePtr argv, MsgId message, FPObj me)
  246. { float_methods(FN_RandomX,argc,argv,message,me); }
  247.  
  248. pascal long MethodLog21(long argc, TyValuePtr argv, MsgId message, FPObj me)
  249. { float_methods(FN_Log21,argc,argv,message,me); }
  250.  
  251. pascal long MethodExp21(long argc, TyValuePtr argv, MsgId message, FPObj me)
  252. { float_methods(FN_Exp21,argc,argv,message,me); }
  253.  
  254. /*--- end of FP_main.c ---*/
  255.